<?php

/*
	info: 前台用户管理
	date: 2011-11-30
*/

if (!defined('ADMIN_WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

/*
用户出生地址数据描述
$birth_address = array(
	'id_list' => array(), //出生地对应的地区id
	'area_text'   => array(), //出生地对应的地区名字
);
*/
$webglobal['file_user'] = array();

$webglobal['file_user']['status'] = array(
	-1 => '禁止登录',
	0  => '未认证',
	1  => '认证通过'
);

//批量删除用户
if ($webglobal['mode']=='userup') {
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}


	global_CheckPost(
				array(
					array('delid[]','int','',''),
			));

	if (!empty($webglobal['CPP']['delid'])) {
		$tempDelSql = $db->BuildInSql($webglobal['CPP']['delid']);
		//删除用户收藏
		$db->Delete('coupon_favorites', 'user_id IN ('. $tempDelSql .')');
		$db->Delete('company_favorites', 'user_id IN ('. $tempDelSql .')');
		//删除用户评论
		//$db->Delete('company_comment', 'user_type = 1 AND user_id IN ('. $tempDelSql .')');
		//$db->Delete('coupon_comment', 'user_type = 1 AND user_id IN ('. $tempDelSql .')');
		//用户积分兑换表
		$db->Delete('integral_order', 'user_id IN ('. $tempDelSql .')');
		//删除用户的头像附件
		$query = $db->Query("SELECT `id`, `avatar` FROM {$webconfig['dbpre']}user WHERE id IN ({$tempDelSql})");
		while ($tempdata = $db->FetchArray($query)) {
			if ($tempdata['avatar']) {
				@unlink(WEBFILEPATH.$tempdata['avatar']);
			}
		}
		//删除用户
		$db->Delete('user', 'id IN ('.$tempDelSql.')');
		admin_AddLog("批量删除用户|id:" . implode(',', $webglobal['CPP']['delid']));
		admin_ShowMessage('批量删除用户成功。', ADMIN_WEBURLPATH."/index.php?mode=user&{$webrequest['gourl']}");
	} else {
		admin_ShowMessage("没有任何条目被删除，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=user&{$webrequest['gourl']}");
	}
} elseif ($webglobal['mode']=='useredit') {
	$webrequest['editid'] = isset($webrequest['editid']) ? intval($webrequest['editid']) : 0;
	if ($webrequest['editid'] <= 0) {
		admin_ShowMessage('不存在该用户。', '', 'BACK');
	}

	$webglobal['file_user']['editinfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}user WHERE id='{$webrequest['editid']}'");
	if (!$webglobal['file_user']['editinfo']) {
		admin_ShowMessage('不存在该用户。', '', 'BACK');
	}

	if (global_CheckSubmit('adminsubmit')) {
		global_CheckPost(
				array(
					array('tel,','str','','',0, ''),
					array('indentitycard,','str','','',0,''),
					array('status,','int','','',0,0),
					array('score,','int','','',0,0),
					array('nickname,','str','','',0,''),
					array('truename,','str','','',0,''),
					array('gender,','int','','',0,0),
					array('birthday,','date','','出生日期必须为日期格式'),
					array('birthday_address[]','int','','',0,0),
					array('birthday_address_name','str','','',0,''),
					array('signhtml,','str','','',0,''),
			));
		if (!empty($webglobal['CPM'])) {
			admin_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {
			//admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=user&{$webrequest['gourl']}");

			$birthday_address = array(
				'id_list' => array(),
				'area_text' => array()
			);
			if (isset($webglobal['CPP']['birthday_address']) && $webglobal['CPP']['birthday_address']) {
				$birthday_address_name = explode(',', $webglobal['CPP']['birthday_address_name']);
				foreach ($webglobal['CPP']['birthday_address'] as $key => $listid) {
					if ($listid) {
						$birthday_address['id_list'][] = $listid;
						$birthday_address['area_text'][] = isset($birthday_address_name[$key]) ? $birthday_address_name[$key] : '';
					} else {
						break;
					}
				}
			}

			$webglobal['CPD']['birth_address'] = serialize($birthday_address);
			$db->Update('user', '', "id='{$webrequest['editid']}'");
			admin_AddLog("修改用户|id: {$webrequest['editid']}; title: {$webglobal['file_user']['editinfo']['username']}");
			admin_ShowMessage('用户修改成功。', ADMIN_WEBURLPATH."/index.php?mode=user&{$webrequest['gourl']}");
		}
	} else {
		$webglobal['file_user']['gourlencode'] = urlencode($webrequest['gourl']);
		$webglobal['file_user']['editinfo']['regtime_show'] = global_FormatTime($webglobal['file_user']['editinfo']['regtime'], 'FD');
		$webglobal['file_user']['editinfo']['logintime_show'] = global_FormatTime($webglobal['file_user']['editinfo']['logintime'], 'FD');
		$webglobal['file_user']['editinfo']['birth_address'] = $webglobal['file_user']['editinfo']['birth_address'] ? unserialize($webglobal['file_user']['editinfo']['birth_address']) : array();
		if (isset($webglobal['file_user']['editinfo']['birth_address']['id_list'])) {
			$webglobal['file_user']['editinfo']['birth_address']['id_list'] = implode(',', $webglobal['file_user']['editinfo']['birth_address']['id_list']);
		} else {
			$webglobal['file_user']['editinfo']['birth_address']['id_list'] = '';
		}

		if (isset($webglobal['file_user']['editinfo']['birth_address']['area_text'])) {
			$webglobal['file_user']['editinfo']['birth_address']['area_text'] = implode(',', $webglobal['file_user']['editinfo']['birth_address']['area_text']);
		} else {
			$webglobal['file_user']['editinfo']['birth_address']['area_text'] = '';
		}

		$webglobal['file_user']['editinfo']['auth_tel'] = showStatus($webglobal['file_user']['editinfo']['auth_tel']);
		$webglobal['file_user']['editinfo']['auth_indentity'] = showStatus($webglobal['file_user']['editinfo']['auth_indentity']);

		admin_StartFromForm();
	}

} elseif ($webglobal['mode']=='user') {

	$webrequest['status'] = isset($webrequest['status']) ? intval($webrequest['status']) : -2;
	$webrequest['gender'] = isset($webrequest['gender']) ? intval($webrequest['gender']) : -1;
	$webrequest['auth_tel'] = isset($webrequest['auth_tel']) ? intval($webrequest['auth_tel']) : -1;
	$webrequest['auth_indentity'] = isset($webrequest['auth_indentity']) ? intval($webrequest['auth_indentity']) : -1;
	$webrequest['searchkey'] = isset($webrequest['searchkey']) ? $webrequest['searchkey'] : '';
	
	$webglobal['file_user']['gourl'] = "page={$webrequest['page']}";
	$webglobal['file_user']['pageurl'] = "";

	$tempsearchsql = '';
	if ($webrequest['searchkey']!='') {
		$tempsearchsql = $db->BuildSearchSql($webrequest['searchkey'], array('username', 'email', 'truename', 'nickname'));
		$webglobal['file_user']['gourl'] .= "&searchkey=".urlencode($webrequest['searchkey']);
		$webglobal['file_user']['pageurl'] .= "&searchkey=".urlencode($webrequest['searchkey']);
	}

	if ($webrequest['status'] > -2) {
		$tempsearchsql .= (($tempsearchsql!='') ? ' AND ' : '')."status='{$webrequest['status']}'";
		$webglobal['file_user']['gourl'] .= "&status={$webrequest['status']}";
		$webglobal['file_user']['pageurl'] .= "&status={$webrequest['status']}";
	}
	if ($webrequest['gender'] > -1) {
		$tempsearchsql .= (($tempsearchsql!='') ? ' AND ' : '')."gender='{$webrequest['gender']}'";
		$webglobal['file_user']['gourl'] .= "&gender={$webrequest['gender']}";
		$webglobal['file_user']['pageurl'] .= "&gender={$webrequest['gender']}";
	}
	if ($webrequest['auth_tel'] > -1) {
		$tempsearchsql .= (($tempsearchsql!='') ? ' AND ' : '')."auth_tel='{$webrequest['auth_tel']}'";
		$webglobal['file_user']['gourl'] .= "&auth_tel={$webrequest['auth_tel']}";
		$webglobal['file_user']['pageurl'] .= "&auth_tel={$webrequest['auth_tel']}";
	}
	if ($webrequest['auth_indentity'] > -1) {
		$tempsearchsql .= (($tempsearchsql!='') ? ' AND ' : '')."auth_indentity='{$webrequest['auth_indentity']}'";
		$webglobal['file_user']['gourl'] .= "&gender={$webrequest['gender']}";
		$webglobal['file_user']['pageurl'] .= "&gender={$webrequest['gender']}";
	}

	$tempsearchsql = $tempsearchsql ? " WHERE {$tempsearchsql}" : '';

	$webglobal['file_user']['gourlencode'] = urlencode($webglobal['file_user']['gourl']);

	$webglobal['file_user']['perpage'] = 15;
	$webglobal['file_user']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}user{$tempsearchsql}");

	if ($webglobal['file_user']['datanum'] > 0) {
		$webglobal['page'] = global_Page($webglobal['file_user']['datanum'], $webglobal['file_user']['perpage'], $webrequest['page'], ADMIN_WEBURLPATH."/index.php?mode=user{$webglobal['file_user']['pageurl']}&page=@@");
		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_user']['perpage'];
		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}user{$tempsearchsql} LIMIT $tempstartnum, {$webglobal['file_user']['perpage']}");
		$temptr = 1;
		$webglobal['file_user']['dataarray'] = array();
		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
			$tempdata['regtime_show'] = global_FormatTime($tempdata['regtime'], 'FD');
			$tempdata['logintime_show'] = global_FormatTime($tempdata['logintime']);
			$tempdata['auth_tel'] = showStatus($tempdata['auth_tel']);
			$tempdata['auth_indentity'] = showStatus($tempdata['auth_indentity']);
			$tempdata['status'] = showStatus($tempdata['status']);
			$webglobal['file_user']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
}

function showStatus($type) {
	$status = array(
		-1 => array('m-error', '禁止登录'),
		0 => array('m-warn', '未认证'),
		1=> array('m-success', '认证通过')
	);

	return isset($status[$type]) ? $status[$type] : '';
}

template_Display('user', 1);